package com.jingdianjichi.wx.ai;

import java.security.MessageDigest;

public final class QuestionKeyUtil {
    private QuestionKeyUtil(){}

    public static String normalize(String s) {
        if (s == null) return "";
        String t = s.trim()
                .replaceAll("\\s+", " ")
                .replaceAll("[\\p{Punct}“”‘’、，。！？；：【】（）…]", "");
        return t.toLowerCase();
    }

    public static String md5(String s) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] b = md.digest(s.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte x : b) sb.append(String.format("%02x", x));
            return sb.toString();
        } catch (Exception e) { return Integer.toHexString(s.hashCode()); }
    }

    public static String keyFor(String question) {
        return md5(normalize(question));
    }
}
